/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tuitionmanagement.DAO;

import tuitionmanagement.entity.Role;
import tuitionmanagement.util.DBUtil;
import tuitionmanagement.util.PassEncrypt;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Admin
 */
public class RoleDAO {
   private Connection con; 
    private PassEncrypt pe;
    private ResultSet rs;
    private CallableStatement cst;
    private PreparedStatement pst;
    private String lastError;
    private final String SQL_CREATE;
    private final String SQL_READ;
    private final String SQL_UPDATE;
    private final String SQL_DELETE;

    public RoleDAO(){
        con = null;
        pe = new PassEncrypt();
        rs = null;
        pst = null;
        lastError = "";
        SQL_CREATE = "INSERT INTO [Role](RoleId,RoleName) VALUES(?,?)";
        SQL_READ = "SELECT * FROM [Role]";
        //SQL_UPDATE = "SELECT * FROM [Role] WHERE RoleId = ?";
        SQL_UPDATE = "update Role set RoleName=? where RoleID=?";
        SQL_DELETE = "DELETE FROM Role WHERE RoleId = ?";
    }


    public ArrayList<Role> readByAll() {
        ArrayList<Role> list = new ArrayList<Role>();
        con = DBUtil.getConn();
        try {
            pst = con.prepareStatement(SQL_READ);
            rs = pst.executeQuery();
            while (rs.next()) {
                Role b = new Role();
                b.setRoleId(rs.getInt(1));
                b.setRoleName(rs.getString(2));
                list.add(b);
            }
        } catch (SQLException ex) {
            setLastError("SQL Error!");
        }
            finally {
            DBUtil.closeConn(con);
        }
        return list;
    }


    public boolean create(Role b) {

        try {
            con =  DBUtil.getConn();
            pst = con.prepareStatement(SQL_CREATE);
            pst.setInt(1, b.getRoleId());
            pst.setString(2, b.getRoleName());


            if (pst.executeUpdate() > 0) {
                setLastError("Add Role Successfully!");
                return true;
            }
        } catch (SQLException ex) {
            setLastError(ex.getMessage());
        }
        finally {
            DBUtil.closeConn(con);
        }
        return false;
    }


    public boolean update(Role b) {
        con =  DBUtil.getConn();
        try {
            int effectedRows=0;
            pst = con.prepareStatement(SQL_UPDATE, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
           pst.setString(1, b.getRoleName());
            pst.setInt(2, b.getRoleId());
            
            effectedRows = pst.executeUpdate();
           // rs.first();
            //rs.updateString(2, b.getRoleName());
            //rs.updateRow();
            setLastError("Update Role successfully!");
            if(effectedRows>0) return true;
        } catch (SQLException ex) {
            setLastError(ex.getMessage());
        } finally {
           DBUtil.closeConn(con);
        }
        return false;
    }


    public boolean delete(int roleId) {
        con =  DBUtil.getConn();
        try {
            pst = con.prepareStatement(SQL_DELETE, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            pst.setInt(1, roleId);
            if (pst.executeUpdate() > 0) {
                setLastError("Delete Role successfully!");
                return true;
            }
        } catch (SQLException ex) {
            setLastError(ex.getMessage());
        } finally {
           DBUtil.closeConn(con);
        }
        return false;
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }



}
